<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>获取从今天往前推，一周的星期及对应的日期</title>
</head>
<body>

<script>
    //获取今天是几号？星期几？
   function getDay_Week() {
       var mydata = new Date();
       var hao = mydata.getDate();//得到今天是几号
       var weeknum = mydata.getDay();//今天是周几

       var weekfont;
       switch(weeknum){
           case 0:weekfont='日'; break;
           case 1:weekfont='一'; break;
           case 2:weekfont='二'; break;
           case 3:weekfont='三'; break;
           case 4:weekfont='四'; break;
           case 5:weekfont='五'; break;
           case 6:weekfont='六'; break;
       }
       console.log('今天是'+hao+'号 周'+weekfont);
   }
   getDay_Week();



//得到今天是周几（将数字周几转为汉字的）
    function getTodayWeekFont() {
        var mydata = new Date();
        var weeknum = mydata.getDay();//今天是周几
        var weekfont;
        switch(weeknum){
            case 0:weekfont='日'; break;
            case 1:weekfont='一'; break;
            case 2:weekfont='二'; break;
            case 3:weekfont='三'; break;
            case 4:weekfont='四'; break;
            case 5:weekfont='五'; break;
            case 6:weekfont='六'; break;
        }
        return weekfont;
    }
    console.log('今天是周'+getTodayWeekFont());

//  此方法已封装好：可以拿到从今天开始，往前六天的号数和星期
    function getCalend() {
        var mydata = new Date();
        var today = mydata.getTime();
        var Calend = new Object();                                               //创建日历一个对象
        for(var i=0;i<7;i++){
            Calend[i] = new Object();                                             //把日历对象扩展成一个数组对象
            Calend[i].day = new Date(today-24*60*60*1000*i).getDate();           //用今天的毫秒数减去n天一共的毫秒数，就拿到n天前的日期毫秒数。new Date（毫秒数）.getDate()
            switch(new Date(today-24*60*60*1000*i).getDay()){                    //用switch把星期的数字弄成文字。
                case 0: Calend[i].week='日'; break;                              //用for循环，把这些装到一个对象里。
                case 1: Calend[i].week='一'; break;
                case 2: Calend[i].week='二'; break;
                case 3: Calend[i].week='三'; break;
                case 4: Calend[i].week='四'; break;
                case 5: Calend[i].week='五'; break;
                case 6: Calend[i].week='六'; break;
            }
        }
        return Calend;
    }
    console.log(getCalend());

//  使用类的方法与上面，不同
    function getCalend2() {
        var Calend = new Object();                                               //创建日历一个对象
        var Canlend = {
        };

        var mydata = new Date();
        var today = mydata.getTime();
        for(var i=0;i<7;i++){
            Calend[i] = {
                day:null,
                week:null
            };                                             //把日历对象扩展成一个数组对象
            Calend[i].day = new Date(today-24*60*60*1000*i).getDate();           //用今天的毫秒数减去n天一共的毫秒数，就拿到n天前的日期毫秒数。new Date（毫秒数）.getDate()
            switch(new Date(today-24*60*60*1000*i).getDay()){                    //用switch把星期的数字弄成文字。
                case 0: Calend[i].week='日'; break;                              //用for循环，把这些装到一个对象里。
                case 1: Calend[i].week='一'; break;
                case 2: Calend[i].week='二'; break;
                case 3: Calend[i].week='三'; break;
                case 4: Calend[i].week='四'; break;
                case 5: Calend[i].week='五'; break;
                case 6: Calend[i].week='六'; break;
            }
        }
        return Calend;
    }
    console.log(getCalend());
</script>
</body>
</html>



